<?php
require_once('./includes/common.php');
require_once(SYSTEM_ROOT."eshanghu_weixin/eshanghu.php");
$notify_url = "http://".$conf['local_domain']."/eshanghu_notify.php";
$config = [
	'app_key' => $conf['wx_eshanghu_app_key'],
	'app_secret' => $conf['wx_eshanghu_app_secret'],
	'sub_mch_id' => $conf['wx_eshanghu_sub_mch_id'],
	'notify' => $notify_url,
];

$eshanghu = new Eshanghu($config);
file_put_contents('./eshanghu.txt', json_encode($_POST));

$params = [
	'out_trade_no' => $_POST['out_trade_no'],
	'sign' => $_POST['sign'],
	'order_sn' => $_POST['order_sn'],
	'total_fee' => $_POST['total_fee'],
	'status_text' => $_POST['status_text'],
	'status' => $_POST['status'],
	'attach' => $_POST['attach'],
];
$result = $eshanghu->callback($params);

if($result) {
	//商户订单号
	$out_trade_no = $params['out_trade_no'];
    $totalFee = $params['total_fee']/100;

    $srow=$DB->query("SELECT * FROM pay_order WHERE trade_no='{$out_trade_no}' limit 1")->fetch();
	if($srow['status']==0 and $totalFee == $srow['money']){
		$date = date('Y-m-d H:i:s');
		$DB->query("update `pay_order` set `status` ='1',`endtime` ='$date' where `trade_no`='$out_trade_no'");
		processOrder($srow);
		$url=creat_callback($srow);
		curl_get($url['notify']);
		proxy_get($url['notify']);
	}
	echo "success";
}
else {
    echo "fail";
}
?>